using System.Collections;
using System.Collections.Generic;
using BasicScript;
using Game.Const;
using Game.Core;
using Game.Core.Loader;
using Game.Main.Event;
using UnityEngine;

namespace Game.Manager
{
	public class AssetBundleManager : Singleton<AssetBundleManager>
	{
		public AssetBundle mainAssetBundle;

		public AssetBundleManifest manifest;

		public Dictionary<string, AssetBundle> commonResources;

		public Dictionary<string, AssetBundleData> loadedResources;

		public Dictionary<string, Shader> shaders;

		public GameObject characterResource;

		public List<Sprite> topsIcons;

		public Font font;

		public int loadingResNum;

		private float lastUnloadTime;

		private const string CHARACTER_MODEL = "charactermodel";

		private int needLoadResNum;

		public AssetBundleManager()
		{
			loadedResources = new Dictionary<string, AssetBundleData>();
			commonResources = new Dictionary<string, AssetBundle>();
			shaders = new Dictionary<string, Shader>();
			shaders.Add("Sprites/Diffuse", Shader.Find("Sprites/Diffuse"));
			shaders.Add("Sprites/DiffuseFill", Shader.Find("Sprites/DiffuseFill"));
			shaders.Add("Sprites/DiffuseWater", Shader.Find("Sprites/DiffuseWater"));
			shaders.Add("Sprites/DiffuseWaterFill", Shader.Find("Sprites/DiffuseWaterFill"));
			//commonResources.Add("assets/font/myfont", null);
		}

		public override void Init()
		{
			Client.Instance.StartCoroutine(StartLoadCommonResources());
		}

		private IEnumerator StartLoadCommonResources()
		{
			AssetBundleCreateRequest www2 /*= AssetBundle.LoadFromFileAsync(ClientConfig.GetPath("compose"))*/;
			//yield return www2;
			//if (www2.assetBundle != null)
			//{
			//	mainAssetBundle = www2.assetBundle;
			//	manifest = mainAssetBundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
			//	mainAssetBundle.Unload(false);
			//}
			//else
			//{
			//	Fire(new BaseEvent(LoadEvent.LOAD_ERROR));
			//}
			loadedResources = new Dictionary<string, AssetBundleData>();
			List<string> keys = new List<string>(commonResources.Keys);
			for (int i = 0; i < keys.Count; i++)
			{
				if (keys[i].StartsWith("assets/font"))
				{
					www2 = AssetBundle.LoadFromFileAsync(ClientConfig.GetPath(keys[i]));
					yield return www2;
					if (www2.assetBundle != null)
					{
						font = (Font)www2.assetBundle.LoadAsset("myfont.ttf");
						commonResources[keys[i]] = www2.assetBundle;
					}
					else
					{
						Trace.Log("字体资源加载失败");
					}
				}
			}
			Fire(new BaseEvent(LoadEvent.LOAD_COMPLETE));
		}

		public void LoadCharacterResources()
		{
			if (characterResource != null)
			{
				Fire(new BaseEvent(LoadEvent.LOAD_COMPLETE));
				return;
			}
			needLoadResNum = 0;
			List<string> list = new List<string>();
			list.Add("charactermodel");
			List<ResourceLoader> list2 = new List<ResourceLoader>();
			for (int i = 0; i < list.Count; i++)
			{
				ResourceLoader resourceLoader = new ResourceLoader(PathConst.MODEL + list[i], list[i] + ".prefab");
				resourceLoader.Bind(LoadEvent.LOAD_COMPLETE, OnCharacterResLoadComplete);
				list2.Add(resourceLoader);
				needLoadResNum++;
			}
			for (int j = 0; j < list2.Count; j++)
			{
				list2[j].Load();
			}
		}

		public Sprite GetTopsIcon(int id)
		{
			return null;
		}

		private void OnCharacterResLoadComplete(BaseEvent evt)
		{
			needLoadResNum--;
			ResourceLoader resourceLoader = evt.sender as ResourceLoader;
			if (resourceLoader.resourceName.Contains("charactermodel"))
			{
				characterResource = (GameObject)Object.Instantiate(resourceLoader.resource);
				characterResource.SetActive(false);
                //characterResource.SetActive(true);
            }
			resourceLoader.Destroy();
			if (needLoadResNum <= 0)
			{
				Fire(new BaseEvent(LoadEvent.LOAD_COMPLETE));
			}
		}

		public void ClearAssets(bool isDisposeResources = true)
		{
			loadingResNum = 0;
			if (isDisposeResources)
			{
				foreach (KeyValuePair<string, AssetBundleData> loadedResource in loadedResources)
				{
					if (loadedResource.Value != null)
					{
						loadedResource.Value.Dispose();
					}
				}
				loadedResources.Clear();
			}
			UnloadUnusedAssets(true);
		}

		public Shader GetShader(string name)
		{
			return shaders[name];
		}

		public void UnloadUnusedAssets(bool isForce = false)
		{
			if (isForce || Time.time - lastUnloadTime > 5f)
			{
				lastUnloadTime = Time.time;
				Resources.UnloadUnusedAssets();
			}
		}
	}
}
